package com.kevin.spring.reference;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
@EnableConfigurationProperties(RpcClientProperties.class)
@Slf4j
public class RpcReferenceAutoConfiguration implements EnvironmentAware {

    private Environment environment;
    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Bean
    public SpringRpcBeanPostProcessor getSpringRpcBeanPostProcessor(RpcClientProperties rpcClientProperties){
        if(null==rpcClientProperties){
            log.error("rpcClientProperties is null");
        }
        else{
            //Todo 这里取不到ip port，是不是因为触发的时机不对
            log.info("ip={},port={}",rpcClientProperties.getServerIp(),rpcClientProperties.getServerPort());
        }
        String ip = environment.getProperty("rpc.client.serverIp");
        int port = Integer.parseInt(environment.getProperty("rpc.client.serverPort"));
        log.info("ip={},  port={}",ip,port);
        RpcClientProperties rpcClientProperties2 = new RpcClientProperties();
        rpcClientProperties.setServerIp(ip);
        rpcClientProperties.setServerPort(port);
        return new SpringRpcBeanPostProcessor(rpcClientProperties);

    }
}
